<template>
<div>
    <el-card style="margin: 20px auto">
        <el-form :inline="true" class="demo-form-inline messageInput">
            <el-form-item label="地块编号">
                <el-input v-model="params['landInfo.landCode']" maxlength="30" show-word-limit placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="地块名称">
                <el-input v-model="params['landInfo.landName']" maxlength="30" show-word-limit placeholder="请输入"></el-input>
            </el-form-item>

            <SelectTown @on-change="getTownId">镇街</SelectTown>
            <SelectVillage @on-change="getVillageId" :townId="townId">乡村</SelectVillage>
            <landType @on-change="getLandTypeCode">地块类型</landType>
            <landProduct @on-change="getlandProductCode">农产品分类</landProduct>
            <el-form-item class="btn">
                <el-button type="primary" @click="searchSubject()">查询</el-button>
                <el-button @click="resetValues()">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <!-- 搜索部分结束 -->

    <el-table ref="multipleTable" :data="maplandIdList" tooltip-effect="dark" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column property="landId" label="图斑ID"></el-table-column>
        <el-table-column property="landName" label="地塘块命名"></el-table-column>
        <el-table-column property="landTownName" label="镇街"></el-table-column>
        <el-table-column property="landVillageName" label="村居"></el-table-column>
        <el-table-column property="landTypeName" label="类型"></el-table-column>
        <el-table-column property="landProductName" label="农产品分类"></el-table-column>
        <el-table-column property="landProductArea" label="面积(亩)"></el-table-column>
        <el-table-column property="subjectNames" label="所属主体">
            <template slot-scope="scope">
                {{scope.row.subjectNames}}
            </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
            <template slot="header">
                <el-button type="primary" size="small" @click="handleEdit">批量编辑</el-button>
                <el-button type="primary" size="small" @click="handleAudit">批量审核</el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-dialog title="批量审核" append-to-body :visible.sync="handleAuditVisible">
        <el-card shadow="never">
            <el-form ref="form" :model="audit" label-width="120px">
                <el-form-item label="审核状态">
                    <el-select v-model="audit.value" placeholder="请选择">
                        <el-option v-for="item in auditOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核意见">
                    <el-input type="textarea" placeholder="请输入审核意见(允许为空)" v-model="audit.textarea"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="BatchReview">批量审核</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </el-dialog>

    <el-dialog title="批量编辑" append-to-body :visible.sync="handleEditVisible" width="30%" >
        <el-card shadow="never">
            <el-form ref="form" :model="audit" label-width="120px" label-position="top">
                 <el-form-item label="村经济社小组">
                    <el-input type="text" v-model="LandParcelInfo.villageGroupName"></el-input>
                </el-form-item>
                <el-form-item label="经营主体">
                    <el-input type="text"  v-model="LandParcelInfo.subjects">
                         <relevancy slot="append" @on-success="getSubject" :landCodeT="landCodeArray">添加记录</relevancy>   
                    </el-input>
                </el-form-item>
                <landProduct @on-change="getlandProductCode" style="width:100%">农产品分类</landProduct>
                <el-form-item label="品种">
                    <el-input type="text" v-model="LandParcelInfo.landOtherType"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="BatchEdit">批量编辑</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </el-dialog>

</div>
</template>

<script>
export default {
    props: {
        mapland: {
            type: Array,
            required: true,
        },
        batchrandom: {
            type: Number,
            required: true,
            default: 0
        },
        maplandListpage: {
            type: Object,
            required: true,
            default: function () {
                return {};
            },
        },
        currentTask: {
            type: Object,
            default: function () {
                return {};
            },
        },
        subjectData: {
            type: Array,
            default: function () {
                return [];
            },
        }
    },
    data() {
        return {
            LandParcelInfo: {
                villageGroupName:"",
                subjects:"",
                landOtherType:""
            },
            page: {},
            search: {
                pageSize: 1,
                pageNumber: 5,
            },
            auditOptions: [{
                    value: "2",
                    label: "通过",
                },
                {
                    value: "3",
                    label: "不通过",
                },
            ],
            multipleSelection: [],
            townId: "",
            VillageId: "",
            TypeId: "",
            LandTypeCode: "",
            ProductCode: "",
            handleAuditVisible: false,
            handleEditVisible: false,
            landCodeArray: [],
            audit: {},
            cascaderOptionsOne: [],
            cascaderOptionsTwo: [],
            subjectArray: [],
            params: {
                pageNumber: 1,
                pageSize: 10,
                "landInfo.landTownCode": "",
                "landInfo.landVillageCode": "",
                "landInfo.landProduce": "",
                "landInfo.land_product": "",
                "landInfo.landCode": "",
                "landInfo.landType": "",
                "landInfo.landName": "",
                landCodes: "",
                systemTypes: localStorage.getItem('SYS_TYPE'),
                "collectTaskLand.planId": "",
            }
        };
    },
    computed: {
        page: {
            get() {
                return this.maplandListpage
            },
            set(page) {
                this.maplandListpage = page
            }
        },
        maplandIdList: {
            get() {
                this.params.landCodes = this.maplandListpage.landCodes
                this.landCodeArray = this.maplandListpage.landCodes.split(',')
                this.toggleSelection(this.mapland);
                return this.mapland
            },
            set(arr) {
                this.mapland = arr
            }
        }
    },
    methods: {
        getSubject(subjectArray) {
            this.LandParcelInfo.subjects = subjectArray.subjectName
            console.log(subjectArray,this.LandParcelInfo.subjects);
            [subjectArray].forEach((item) => {
                this.subjectArray.push(item)
            })
        },
        handleChange(value) {
            this.LandParcelInfo.landType = value[value.length - 1];
            console.log(this.LandParcelInfo.landType);
        },
        handleChangeTwo(value) {
            console.log(value);
            this.LandParcelInfo.landProduct = value[value.length - 1];
            console.log(this.LandParcelInfo.landProduct);
        },
        BatchEdit() {
            let landCodeArray = '';
            let subjectArray = [];
            if (this.landCodeArray instanceof Array) {
                landCodeArray = this.landCodeArray.join(",")
            } else {
                landCodeArray = this.landCodeArray
                this.subjectArray.forEach((item) => {
                    subjectArray.push({
                        landSubjectBase: item.subjectBase,
                        lsSubjectUseArea: item.lsSubjectUseArea ? item.lsSubjectUseArea : 0,
                        lsYearValue: item.lsYearValue ? item.lsYearValue : 0,
                        lsSubjectCode: item.subjectCode,
                    });
                });
            }

            let currentTask;
            if (this.currentTask) {
                currentTask = this.currentTask.data
            } else {
                this.$message({
                    type: "error",
                    message: "请选择对应任务",
                });
            }

            let BatchEditObject = {
                landIds: landCodeArray,
                landType: this.LandParcelInfo.landType,
                landProduct: this.ProductCode,
                villageGroupName: this.LandParcelInfo.villageGroupName,
                landOtherType: this.LandParcelInfo.landOtherType,
                landStatusQuo: this.LandParcelInfo.landStatusQuo,
                landProduce: this.LandParcelInfo.landProduce,
                landSubjects: subjectArray,
                planId: currentTask.id,
            };
			// debugger
            this.$axios
                .post("rbac/data/CollectTaskLand/batchEdit", {
                    params: BatchEditObject,
                })
                .then((result) => {
					this.searchSubject();
                    if (result.errorCode === "00000") {
                        this.handleEditVisible = false;
                        this.$message({
                            type: "success",
                            message: "批量修改成功",
                        });
                        this.$emit("on-collect", true);
                    } else {
                        this.$message({
                            type: "error",
                            message: result.errorMsg,
                        });
                    }
                    console.log(result);
                }).catch(()=>{})

        },
        BatchReview() {
            console.log(this.multipleSelection);
            if (this.multipleSelection.length) {
                let landIds = [];

                this.multipleSelection.forEach((item) => {
                    landIds.push(item.id);
                });

                this.$axios
                    .post("rbac/LandSubject/auditLand", {
                        params: {
                            landIds: landIds.join(","),
                            auditMemo: this.audit.textarea,
                            auditStatus: this.audit.value,
                        },
                    })
                    .then((res) => {
						this.searchSubject();
                        if (res.errorCode === "00000") {
                            this.$message({
                                type: "success",
                                message: "批量审核成功",
                            });
                            this.$emit("on-collect", true);
                        } else {
                            this.$message({
                                type: "warning",
                                message: res.errorMsg,
                            });
                        }
                    })
                    .catch(()=>{})
            } else {
                this.$message({
                    type: "warning",
                    message: "批量审核必须选中一条",
                });
            }
        },

        handleAudit() {
            if (this.multipleSelection.length) {
                this.handleAuditVisible = true;
            } else {
                this.$message({
                    type: "warning",
                    message: "批量审核必须选中一条",
                });
            }
        },
        handleEdit() {
            this.landCodeArray = []
            if (this.multipleSelection.length) {
                this.multipleSelection.forEach((item) => {
                    this.landCodeArray.push(item.landId);
                });
                this.handleEditVisible = true;
            } else {
                this.$message({
                    type: "warning",
                    message: "批量编辑必须选中一条",
                });
            }
        },
        handleSizeChange(pageSize) {
            this.search.pageSize = pageSize;
        },
        currentchange(pageNumber) {
            this.search.pageNumber = pageNumber;
        },
        toggleSelection(rows) {
            this.$nextTick(() => {
                if (rows) {
                    rows.forEach((row) => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                    this.handleSelectionChange(rows);
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        searchSubject() {
            let currentTask;
            if (this.currentTask) {
                currentTask = this.currentTask.data
            } else {
                this.$message({
                    type: "error",
                    message: "请选择对应任务",
                });
            }

            this.params["collectTaskLand.planId"] = currentTask.id

            if (this.params['landInfo.landTownCode'] === this.params['landInfo.landVillageCode']) {
                this.params['landInfo.landVillageCode'] = ""
            }
            this.$axios.get('rbac/data/CollectTaskLand/list', {
                    params: this.params
                })
                .then((res) => {
                    if (res.errorCode === '00000') {
                        this.maplandIdList = res.data.list
                    }

                })
        },
        resetValues() {
            this.townId = '440605'
            this.params = {
                pageNumber: 1,
                pageSize: 10,
                "landInfo.landTownCode": "",
                "landInfo.landVillageCode": "",
                "landInfo.landProduce": "",
                "landInfo.land_product": "",
                "landInfo.landCode": "",
                "landInfo.landType": "",
                "landInfo.landName": "",
                landCodes: "",
                systemTypes: localStorage.getItem('SYS_TYPE'),
                "collectTaskLand.planId": this.currentTask.data.id,
            }
            this.searchSubject()
        },
        getTownId(id) {
            this.townId = id;
            this.params['landInfo.landTownCode'] = this.townId
        },
        getVillageId(id) {
            this.VillageId = id;
            this.params['landInfo.landVillageCode'] = this.VillageId
        },
        getLandTypeCode(id) {
            this.LandTypeCode = id;
            this.params['landInfo.landType'] = this.LandTypeCode
        },
        getlandProductCode(id) {
            this.ProductCode = id;
            this.params['landInfo.land_product'] = this.ProductCode
        },
    },
};
</script>
